package day13;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//可缓存线程池
public class Demo {
    public static void main(String[] args) {
//        ExecutorService es=
//                Executors.newCachedThreadPool();
//        for(int i=0;i<100;i++){
//            es.execute(()->{
//                System.out.println(Thread.currentThread().getName());
//            });
//        }
//        es.shutdown();
        //a.参考课上讲述的定长线程池的案例 通过可缓存线程池 去完成下列需求
        //现在餐厅生意越来越好了，老板直接跟大厨说，你能炒多快，
        // 就炒多快，灶台随时可以
        //加，好好干，工资不会少。同样用线程池模拟炒五个菜的过
        // 程，并且统计炒完共计花费了多久

        String[] names={"辣椒炒肉","鸡脚","小鸡炖蘑菇","甲鱼","黄鳝","a","b","c","d","e"};
        long[] times={1000,3000,5000,5000,2000,1000,2000,3000,4000,6000};
        ExecutorService es=
                Executors.newCachedThreadPool();
        long start=System.currentTimeMillis();
        for(int i=0;i<names.length;i++){
            int index=i;
            es.execute(()->{
                System.out.println("开始做:"+names[index]);
                try {
                    Thread.sleep(times[index]);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("已经做好:"+names[index]);
            });
        }
        es.shutdown();
        while(true){
            if(es.isTerminated()){
                long stop=System.currentTimeMillis();
                System.out.println("所有菜都抄完了:"+
                        (stop-start)+"ms");
                break;
            }
        }
    }
}
